<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Adds the new data including a file data to the XmuSlh.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.23
 * 
 * 0.2)  Inherited from Addaction.
 * 0.21) Adds the 'category' validation.
 * 0.22) Adds the editor feature.
 * 0.23) Adds the direction after added successfully.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_addaction.php';

require_once INSTALLDIR . '/widgets/slh_form_addassociationuser.php';

require_once INSTALLDIR . '/classes/User.php';
require_once INSTALLDIR . '/classes/Association.php';


class AddassociationuserAction extends AddAction
{
	function title()
	{
		return '添加社团用户';
	}
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_ASSOCITION)) {
			return false;
		}
		
		return parent::prepare($args);
	}
	
	/**
	 * Add the editor stylesheet.
	 */
	function showStylesheets()
	{
		parent::showStylesheets();
		
		$this->cssLink('theme/base/css/jquery.Jcrop.css', 'base', 'screen, projection, tv');
	}
	
	/**
	 * Add the editor scripts.
	 */
	function showScripts()
	{
		parent::showScripts();
	
		$this->script('kindeditor/kindeditor.min.js');
		$this->script('kindeditor/kindeditor.go.js');
	}
	
	
	/**
	 * Shows the guidance.
	 * Overriding.
	 */
	function showGuidance()
	{
		$this->element('h3', null, '添加一个新的社团(用户)');
				
		$this->instruction('呃,说明下添加的具体是什么东东,怎么添加好了.');
		
		// If added successfully, then?
		if ($this->added) {
			$this->element('p', 'cls_p_success', '添加成功!');
			// Or show a nav.
		}
	}
	
	/**
	 * Shows the widgets like form, or etc.
	 * Overriding.
	 */
	function showForm()
	{
		$form = new AddassociationuserForm($this, $this->args);
		$form->show();
	}	
	
	/**
	 * Try to add the association into the database.
	 */
	function tryAdd()
	{
		// CSRF protection and Refreshing-post protection.
		$this->checkSessionToken();
		
		$username	= $this->getTrimmedArg('username');
		$cnname		= $this->getTrimmedArg('cnname');
		$enname		= $this->getTrimmedArg('enname');
		$category	= $this->getTrimmedArg('category');
		$pinyin		= $this->getTrimmedArg('pinyin');
		$rendered	= $this->getTrimmedArg('rendered');
		$weibo_id	= $this->getTrimmedArg('weibo_id');
		
// TODO: Validates the data.
		if (empty($username)) {
			$this->showAction('请输入用户名.');
		} else if (empty($cnname)) {
			$this->showAction('请输入社团中文名称.');
		} else if ( !Association::validateCategory($category)) {
			$this->showAction('请选择社团的种类.');
		} else {
			if (get_magic_quotes_gpc()) {
				$rendered = stripslashes($rendered);
			}
			$rendered_encode = slh_compress($rendered);
			
			// Adds into the database.
			$association_id = Association::add(array('username' => $username,
											 		 'cnname' => $cnname,
													 'enname' => $enname,
													 'category' => $category,
											 		 'pinyin' => $pinyin,
											 		 'rendered' => $rendered_encode,
													 'weibo_id' => $weibo_id,
													 'association_rights' => 1024));
			
			if ( !$association_id) {
				$this->showAction('添加用户失败!');
				return;
			}
			
			// Success.
			// Unset the seesion token preventing post-again.
			$this->unsetSessionToken();
			
			// Unset the seted values.
			unset($this->args['username']);
			unset($this->args['cnname']);
			unset($this->args['enname']);
			unset($this->args['category']);
			unset($this->args['pinyin']);
			unset($this->args['weibo_id']);
			unset($this->args['rendered']);
			
			$this->added = true;
			
			// $this->showAction();
			slh_redirect(slh_full_path('index.php/association'), 303);			
		}
		
	}
}

?>